package org.anupta.realm.server.realm.packet.login.answer;

import java.util.ArrayList;
import java.util.List;

import org.anupta.realm.main.Main;
import org.anupta.realm.server.inter.client.InterClient;
import org.anupta.realm.server.inter.client.InterClient.Population;
import org.anupta.realm.server.inter.client.InterClient.State;
import org.anupta.realm.server.realm.client.RealmClient;
import org.anupta.realm.server.realm.packet.RealmPacket;
import org.anupta.realm.utils.others.Config;

public class HostList extends RealmPacket {
	public HostList(RealmClient client) {
		super(client);
		try{
		pack();
		doPacket();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void unpack() {
	}

	@Override
	public void pack() {
		packetOut.append("AH");
		List<Byte> done = new ArrayList<Byte>();
		for(InterClient client : Main.inter.getAllClient()){
			if(packetOut.length() > 2){
				packetOut.append("|");
			}
			packetOut.append(client.getGuid())
			.append(";")
			.append(client.getState().getState())
			.append(";")
			.append(client.getPop().getPopulation())
			.append(";")
			.append("1");//Allow no subscribe
			done.add((byte) client.getGuid());
		}
		for(int serv : Config.GAME_GUID){
			if(!done.contains((byte)serv)){
				if(packetOut.length() > 2){
					packetOut.append("|");
				}
				packetOut.append(serv)
				.append(";")
				.append(State.OFFLINE.getState())
				.append(";")
				.append(Population.FULL.getPopulation())
				.append(";")
				.append("1");//Allow no subscribe
			}
		}
	}

	@Override
	public void doPacket() {
		client.send(packetOut.toString());
		Main.out.println(new StringBuilder("HostList packet sended to : ").append(client.getIp()).toString());
	}
}
